package Control;

import Database.*;
import Model.*;
import javax.imageio.ImageIO;
import javax.swing.*;

import oracle.jdbc.dcn.TableChangeDescription.TableOperation;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;

public class SalesAssistantMain extends JFrame implements ActionListener{
	private JButton sale, salReturn, view, logout;
	private JTextField id;
	private JPanel logo, sales, rightPanel, leftPanel, salesPanel;
	private JLabel logoPicture;
	private TableOperations to;
	private SalesAssistant sa;
	private ImageIcon icon = new ImageIcon("frameIcon.png");

	public SalesAssistantMain(SalesAssistant sa, final TableOperations to) 
	{
		this.sa = sa;
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();

		setIconImage(icon.getImage());
		setTitle("MDL - Sales Assistant");
		setSize(550, 240);
		setLocationRelativeTo(null);
		setResizable(true);
		this.setLayout(layout);
		this.to = to;
		
		addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent windowEvent) {
				to.close();
				LoginGUI lg = new LoginGUI();
				lg.setVisible(true);
			}
		});
		
		BufferedImage myPicture = null;
		Insets insets = new Insets(5, 150, 5, 3);

		// Logo
		try {
			myPicture = ImageIO.read(new File("logo.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		logo = new JPanel();
		logoPicture = new JLabel(new ImageIcon(myPicture));
		logo.add(logoPicture);
		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 1;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		this.add(logo, gc);

		insets = new Insets(5, 30, 5, 30);

		// left Panel
		leftPanel = new JPanel();
		leftPanel.setLayout(layout);

		id = new JTextField(20);
		id.setText("Welcome " + sa.getFname().trim() + " "
				+ sa.getLname().trim());
		id.setEditable(false);
		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		leftPanel.add(id, gc);

		logout = new JButton("Logout");
		logout.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 2;
		gc.weighty = 0;
		gc.weightx = 0;
		insets = new Insets(5, 40, 5, 40);
		gc.insets = insets;
		leftPanel.add(logout, gc);

		gc.gridx = 1;
		gc.gridy = 2;
		gc.weighty = 2;
		gc.weightx = 1;
		this.add(leftPanel, gc);

		insets = new Insets(5, 1, 10, 1);

		// Right Panel
		rightPanel = new JPanel();
		rightPanel.setLayout(layout);

		// SalesPanel
		salesPanel = new JPanel();
		salesPanel.setBorder(BorderFactory.createTitledBorder("Sales"));
		salesPanel.setLayout(layout);

		sale = new JButton("Sale");
		gc.gridx = 1;
		gc.gridy = 0;
		gc.weighty = 0;
		gc.weightx = 1;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		salesPanel.add(sale, gc);
		sale.addActionListener(this);

		view = new JButton("View Loyalty Card");
		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 1;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		salesPanel.add(view, gc);
		view.addActionListener(this);

		insets = new Insets(5, 0, 5, 10);

		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		rightPanel.add(salesPanel, gc);

		gc.gridx = 2;
		gc.gridy = 2;
		gc.weighty = 2;
		gc.weightx = 2;
		this.add(rightPanel, gc);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == logout) 
		{
			to.close();
			LoginGUI gui = new LoginGUI();
			gui.setVisible(true);
			this.dispose();
		} else if (e.getSource() == view) {
			CardView c = new CardView(to);
			c.setVisible(true);
		} else if (e.getSource() == sale) {

			Sales s = new Sales(sa);
			s.setVisible(true);
		}
	}
}

